package top.chukongxiang.monitorservice.config;

import top.chukongxiang.monitorservice.jna.HCNetSDK;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 楚孔响
 * @version 1.0
 * @date 2023/10/25 15:26
 */
public class SdkServerContextHolder {

    private static final Map<Long, Integer> SERVER_USER_ID_MAP = new HashMap<>();
    private static final Map<Long, HCNetSDK.NET_DVR_DEVICEINFO_V40> SERVER_DEVICE_INFO_MAP = new HashMap<>();
    private static final Map<Long, Integer> SERVER_PLAY_CACHE = new HashMap<>();
    /**
     * 获取SDK lUserId
     * @param serverId 服务id
     * @return
     */
    public static Integer getUserId(Long serverId) {
        return SERVER_USER_ID_MAP.get(serverId);
    }

    /**
     * 获取硬盘数量
     * @param serverId
     * @return
     */
    public static Byte getDiskCount(Long serverId) {
        HCNetSDK.NET_DVR_DEVICEINFO_V40 deviceInfoV40 = SERVER_DEVICE_INFO_MAP.get(serverId);
        return deviceInfoV40 == null ? null : deviceInfoV40.struDeviceV30.byDiskNum;
    }

    /**
     * 获取登录设备的类型
     * @param serverId
     * @return
     */
    public static Integer getDvrType(Long serverId) {
        HCNetSDK.NET_DVR_DEVICEINFO_V40 deviceInfoV40 = SERVER_DEVICE_INFO_MAP.get(serverId);
        return deviceInfoV40 == null ? null : deviceInfoV40.struDeviceV30.byDVRType & 0xFF;
    }

    /**
     * 获取数字通道起始通道号
     * 0表示无数字通道，比如DVR或IPC
     * @param serverId
     * @return
     */
    public static Byte getStartDChannelNo(Long serverId) {
        HCNetSDK.NET_DVR_DEVICEINFO_V40 deviceInfoV40 = SERVER_DEVICE_INFO_MAP.get(serverId);
        return deviceInfoV40 == null ? null : deviceInfoV40.struDeviceV30.byStartDChan;
    }

    /**
     * 获取设备最大数字通道个数
     * @param serverId
     * @return
     */
    public static Byte getDChannelCount(Long serverId) {
        HCNetSDK.NET_DVR_DEVICEINFO_V40 deviceInfoV40 = SERVER_DEVICE_INFO_MAP.get(serverId);
        return deviceInfoV40 == null ? null : deviceInfoV40.struDeviceV30.byHighDChanNum;
    }

    /**
     * 模拟通道的起始通道号，从1开始
     * @param serverId
     * @return
     */
    public static Byte getStartChannelNo(Long serverId) {
        HCNetSDK.NET_DVR_DEVICEINFO_V40 deviceInfoV40 = SERVER_DEVICE_INFO_MAP.get(serverId);
        return deviceInfoV40 == null ? null : deviceInfoV40.struDeviceV30.byStartChan;
    }

    /**
     * 设备模拟通道个数，数字（IP）通道最大个数为byIPChanNum + byHighDChanNum*256
     * @param serverId
     * @return
     */
    public static Byte getChannelCount(Long serverId) {
        HCNetSDK.NET_DVR_DEVICEINFO_V40 deviceInfoV40 = SERVER_DEVICE_INFO_MAP.get(serverId);
        return deviceInfoV40 == null ? null : deviceInfoV40.struDeviceV30.byChanNum;
    }

    public static void set(Long serverId, int lUserId, HCNetSDK.NET_DVR_DEVICEINFO_V40 deviceInfoV40) {
        SERVER_USER_ID_MAP.put(serverId, lUserId);
        SERVER_DEVICE_INFO_MAP.put(serverId, deviceInfoV40);
    }

    public static boolean isPlaying(Long serverId) {
        return SERVER_PLAY_CACHE.containsKey(serverId);
    }
    public static void setPlay(Long serverId, Integer handleId) {
        if (handleId == null) {
            SERVER_PLAY_CACHE.remove(serverId);
        } else {
            SERVER_PLAY_CACHE.put(serverId, handleId);
        }
    }

    public static Integer getPlayHandle(Long serverId) {
        return SERVER_PLAY_CACHE.get(serverId);
    }

    public static void clear() {
        SERVER_USER_ID_MAP.clear();
        SERVER_DEVICE_INFO_MAP.clear();
    }

    public static boolean contains(Long serverId) {
        return SERVER_USER_ID_MAP.containsKey(serverId) && SERVER_DEVICE_INFO_MAP.containsKey(serverId);
    }
}
